package com.aididi.util;

import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xuzexiang
 * @Date: 2018-10-16 0016
 * @Description:
 */
public class WebSocketUtils {
    /**
     * 模拟存储 websocket session 使用
     */
    public static final Map<String,Session> LIVING_SESSIONS_CACHE = new ConcurrentHashMap<>();
    public static void sendMessageAll(String message){
        LIVING_SESSIONS_CACHE.forEach((sessionId,session) -> sendMessage(session,message));
    }

    public static void sendMessage(Session session,String message){
        if(session==null){
            return;
        }
        final RemoteEndpoint.Basic basic = session.getBasicRemote();
        if(basic==null){
            return;
        }
        try {
            basic.sendText(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
